package hr.fer.ztel.rassus.brbljaonica.gui;

import hr.fer.ztel.rassus.brbljaonica.business.BusinessProvider;

import javax.swing.JFrame;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Font;
import java.io.File;

import javax.swing.JLabel;

/**
 * Klasa koja predstavlja prozor za individualne chat-ove<br>
 * Prozor se moze stvoriti ako nam netko posalje poruku ili<br>
 * ako izaberemo nekog iz liste kontakata
 *
 */
public class IndividualChatWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextPane textMessagePane;
	private JTextArea inputTextArea;
	private Chat chat;
	private JButton btnSend;
	private String chatBuddy;
	private boolean isFirstTime;

	/**
	 * @wbp.parser.constructor
	 */
	public IndividualChatWindow(String buddy) {
		initGUI();
		chat = BusinessProvider.createChat(buddy, this);
		chatBuddy = buddy;
		BusinessProvider.addChatWindow(chatBuddy, this);
		setVisible(true);
	}
	
	public IndividualChatWindow(Chat chat) {
		isFirstTime = true;
		initGUI();
		this.chat = chat;
		this.chat.addMessageListener(new MessageListener() {
			
			@Override
			public void processMessage(Chat chat, Message message) {
				if (isFirstTime) {
					chatBuddy = message.getFrom();
					BusinessProvider.addChatWindow(chatBuddy, IndividualChatWindow.this);
					isFirstTime = false;
				}
				textMessagePane.setText(textMessagePane.getText() + StringUtils.parseName(message.getFrom()) + ": " + System.getProperty("line.separator") + message.getBody() + System.getProperty("line.separator"));
			}
		});
		setVisible(true);
	}

	private void initGUI() {
		setSize(new Dimension(500, 500));
		getContentPane().setLayout(null);
		
		JScrollPane messageScrollPane = new JScrollPane();
		messageScrollPane.setBounds(10, 36, 414, 240);
		getContentPane().add(messageScrollPane);
		
		textMessagePane = new JTextPane();
		textMessagePane.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		textMessagePane.setEditable(false);
		messageScrollPane.setViewportView(textMessagePane);
		
		JScrollPane inputScrollPane = new JScrollPane();
		inputScrollPane.setBounds(10, 312, 414, 89);
		getContentPane().add(inputScrollPane);
		
		inputTextArea = new JTextArea();
		inputTextArea.setFont(new Font("Tahoma", Font.PLAIN, 11));
		inputTextArea.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER && !e.isShiftDown()) {
					inputTextArea.setText("");
				}
			}
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					if (e.isShiftDown()) {
						inputTextArea.setText(inputTextArea.getText() + System.getProperty("line.separator"));
					}
					else {
					try {
						textMessagePane.setText(textMessagePane.getText() + "Me: " + System.getProperty("line.separator") + inputTextArea.getText() + System.getProperty("line.separator"));
						chat.sendMessage(inputTextArea.getText());
						inputTextArea.setText("");
					} catch (XMPPException e1) {
						e1.printStackTrace();
					}
					}
				}
			}
		});
		inputScrollPane.setViewportView(inputTextArea);
		
		btnSend = new JButton("Send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					textMessagePane.setText(textMessagePane.getText() + "Me: " + System.getProperty("line.separator") + inputTextArea.getText() + System.getProperty("line.separator"));
					chat.sendMessage(inputTextArea.getText());
					inputTextArea.setText("");
				} catch (XMPPException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnSend.setBounds(335, 412, 89, 23);
		getContentPane().add(btnSend);
		
		JLabel lblMessages = new JLabel("Messages");
		lblMessages.setBounds(10, 11, 47, 14);
		getContentPane().add(lblMessages);
		
		JLabel lblInput = new JLabel("Input");
		lblInput.setBounds(10, 287, 26, 14);
		getContentPane().add(lblInput);
		
		JButton btnSendFile = new JButton("Send File");
		btnSendFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser();
				int result = fc.showDialog(IndividualChatWindow.this, "Select");
				if (result == JFileChooser.APPROVE_OPTION) {
					BusinessProvider.sendFile(chatBuddy, fc.getSelectedFile(), inputTextArea.getText());
					textMessagePane.setText(textMessagePane.getText() + "Me: " + System.getProperty("line.separator") + "Sent file: " + fc.getSelectedFile().getName() + System.getProperty("line.separator") + inputTextArea.getText() + System.getProperty("line.separator"));
					inputTextArea.setText("");
				}
			}
		});
		btnSendFile.setBounds(236, 412, 89, 23);
		getContentPane().add(btnSendFile);
	}
	
	public boolean acceptFile(String user) {
		int result = JOptionPane.showConfirmDialog(this, "Accept incoming file from: " + user, "Incoming file", JOptionPane.YES_NO_OPTION);
		if (result == JOptionPane.YES_OPTION) return true;
		else return false;
	}
	
	public File selectFileDestination(String filename) {
		JFileChooser fc = new JFileChooser();
		File f = new File(filename);
		fc.setSelectedFile(f);
		int result = fc.showSaveDialog(this);
		f.delete();
		if (result == JFileChooser.APPROVE_OPTION) {
			return fc.getSelectedFile();
		}
		else return null;
	}

	public JTextPane getTextMessagePane() {
		return textMessagePane;
	}

	public JTextArea getInputTextArea() {
		return inputTextArea;
	}

	public void setChat(Chat chat) {
		this.chat = chat;
	}
}
